<script lang="ts" setup>
import type { PermissionApi } from '#/api/types';

import { useVbenDrawer } from '@vben/common-ui';

import { message } from 'ant-design-vue';

import { useVbenForm } from '#/adapter/form';
import {
  getPermissionModuleApi,
  updatePermissionApi,
} from '#/api/core/modules/permission';
import { $t } from '#/locales';

// 定义事件
const emit = defineEmits(['success']);

// 表单组件
const [Form, formApi] = useVbenForm({
  schema: [
    {
      component: 'Input',
      fieldName: 'id',
      label: 'ID',
      componentProps: {
        style: {
          display: 'none',
        },
      },
    },
    {
      component: 'Input',
      fieldName: 'name',
      label: $t('system.permission.name'),
      rules: 'required',
      componentProps: {
        placeholder: $t('common.inputText', [$t('system.permission.name')]),
      },
    },
    {
      component: 'Input',
      fieldName: 'code',
      label: $t('system.permission.code'),
      componentProps: {
        placeholder: $t('common.inputText', [$t('system.permission.code')]),
        disabled: true,
      },
    },
    {
      component: 'ApiSelect',
      fieldName: 'parentId',
      label: $t('system.permission.category'),

      componentProps: {
        class: 'w-full',
        placeholder: $t('common.select', [$t('system.permission.category')]),
        api: getPermissionModuleApi,
        afterFetch: (res: any) => {
          const options = [{ label: '根目录', value: '0' }];
          res.data.forEach((item: PermissionApi.PermissionModuleVO) => {
            options.push({
              label: item.label,
              value: item.value,
            });
          });
          return options;
        },
      },
    },
    {
      component: 'Textarea',
      fieldName: 'description',
      label: $t('system.permission.description'),
      componentProps: {
        placeholder: $t('common.inputText', [
          $t('system.permission.description'),
        ]),
        rows: 4,
      },
    },
    {
      component: 'RadioGroup',
      fieldName: 'status',
      label: $t('common.status'),
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
        optionType: 'button',
      },
    },
  ],
  showDefaultActions: false,
});

// 抽屉组件
const [Drawer, drawerApi] = useVbenDrawer({
  title: $t('ui.actionTitle.edit', [$t('system.permission.name')]),

  // 表单确认处理
  async onConfirm() {
    try {
      // 表单验证
      const { valid } = await formApi.validate();
      if (!valid) return false;

      // 获取表单值
      const values = await formApi.getValues();

      // 锁定抽屉
      drawerApi.setState({ loading: true });

      // 调用更新API
      await updatePermissionApi(values as PermissionApi.UpdateParams);

      // 显示成功消息
      message.success(
        $t('ui.actionMessage.updateSuccess', [$t('system.permission.name')]),
      );

      // 触发成功事件，刷新列表
      emit('success');

      // 主动关闭抽屉
      drawerApi.close();
      return true;
    } catch {
      return false;
    } finally {
      drawerApi.setState({ loading: false });
    }
  },

  // 打开关闭处理
  onOpenChange(isOpen) {
    if (isOpen) {
      // 获取数据
      const data = drawerApi.getData<PermissionApi.PermissionVO>();
      if (data) {
        // 设置表单值
        formApi.setValues(data);
      }
    }
  },
});

// 暴露API给父组件
defineExpose({
  drawerApi,
});
</script>

<template>
  <Drawer>
    <Form />
  </Drawer>
</template>
